<?php
/**
 *
 * @author Charles
 * @version 
 */
require_once 'Zend/Loader/PluginLoader.php';
require_once 'Zend/Controller/Action/Helper/Abstract.php';
/**
 * User Action Helper 
 * 
 * @uses actionHelper User_Controller_Action_Helper
 */
class User_Controller_Action_Helper_User extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * @var Zend_Loader_PluginLoader
     */
    public $pluginLoader;
    /**
     * Constructor: initialize plugin loader
     * 
     * @return void
     */
    public function __construct ()
    {
        $this->pluginLoader = new Zend_Loader_PluginLoader();
        $this->auth = Zend_Auth::getInstance();
        $this->UserModel = new User_Model_User();
        $this->RoleModel = new User_Model_Acl_Roles();
    }
    /**
     * Strategy pattern: call helper as broker method
     */
    public function direct ()
    {
        $user = $this->auth->getIdentity();
        if (empty($user)) {
            $user = $this->UserModel->fetchNew();
            $user->role = $this->RoleModel->fetchRow("name = 'guest'");
        } else {
            $user->role = $this->RoleModel->fetchRow("id = {$user->role->id}");
        }
        return $user;
    }
    public function logout ()
    {
        return $this->auth->clearIdentity();
    }
}